package com.doctor.ui.office;

import android.content.ContentValues;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.doctor.bean.OfficeBean;
import com.doctor.comm.AlarmReceiver;
import com.doctor.comm.ConstConfig;
import com.doctor.database.DbOperator;
import com.doctor.ui.R;

public class ModifyInfoFrag extends BaseInfoFrag{

	/**
	 * 修改信息
	 */
	
	private OfficeBean bean;
	private LoadImages thread;
	private Handler handler;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		bean=(OfficeBean) getArguments().getSerializable(ConstConfig.BEAN);
		handler=new Handler();
		thread=new LoadImages();
		
		View view=inflater.inflate(R.layout.modify_info, container, false);
		initiaView(view);
		
		return view;
	}
	
	@Override
	protected void initiaView(View view) {
		// TODO Auto-generated method stub
		super.initiaView(view);
		
		title.setText(bean.getTitle());
		if (bean.getRemindDate()!=null) {
			dateBtn.setText(bean.getRemindDate());
		}
		if (bean.getRemindTime()!=null) {
			timeBtn.setText(bean.getRemindTime());
		}
		if (bean.getDetail()!=null) {
			content.setText(bean.getDetail());
		}
		
		handler.postDelayed(thread,DbOperator.delayTime);
	}
	
	//修改一条记录
	@Override
	protected void modifyRecord(String tableName, ContentValues values) {
		// TODO Auto-generated method stub
		String date=values.getAsString("_remind_date");
		String time=values.getAsString("_remind_time");
		String note=values.getAsString("_title");
		int alarmId=-1;
		if (date!=null && !"".equals(date) && time!=null && !"".equals(time)) {
			alarmId=DbOperator.getInstance(context).getAlarmIdById(tableName, bean.getId());
			if (alarmId>0) {
				//重新设置闹铃
			} else {
				//设置一个新闹铃
				alarmId=getAlarmNextIdByTable(tableName);
				values.put("_alarm_id", alarmId);
			}
		}
		int rows=DbOperator.getInstance(context).updateData(tableName, values, bean.getId());
		if (rows>0) {
			AlarmReceiver.setAlarm(getActivity(), date, time, alarmId, note);
			Toast.makeText(context, "修改成功！", Toast.LENGTH_SHORT).show();
			getActivity().getSupportFragmentManager().popBackStack();
		} else {
			Toast.makeText(context, "修改失败！", Toast.LENGTH_SHORT).show();
		}
	}
	
	/**
	 * 从数据库查询图片uri
	 * @param tableName
	 * @param id
	 */
	private void getImageFromDb(String tableName,int id){
		String images=DbOperator.getInstance(context).selectImages(tableName, id);
		if (images!=null && !"".equals(images)) {
			String[] imageArray= images.split(ConstConfig.IMAGE_SPLIT);
			for (String image : imageArray) {
				beanList.add(image);
			}
			adapter.notifyDataSetChanged();
		}
	}
	
	private class LoadImages extends Thread{
		@Override
		public void run() {
			// TODO Auto-generated method stub
			getImageFromDb(table, bean.getId());
		}
	}
	
}
